<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索内容" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID"  width="55" align="center" prop="id" />
      <el-table-column label="工具名称" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="请求参数" align="center" prop="req_args" :show-overflow-tooltip="true" />
      <el-table-column label="日志内容" align="center" prop="log_text" :show-overflow-tooltip="true" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dicts.funcallLogTypeOptions" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="更新时间" align="center" prop="updated_at" width="180" :formatter="formatterTimeYmdHm"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog title="日志详情" :visible.sync="detailVisible" width="90%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :disabled="false" :model="form"  label-width="120px">
        <el-row>
          <el-form-item prop="description" label="工具名称">
            <el-input readonly v-model="form.description" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="remark" label="备注">
            <el-input readonly v-model="form.remark" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="req_args" label="请求参数">
            <el-input type="textarea" readonly :rows="25" v-model="form.req_args" />
          </el-form-item>
          <el-form-item prop="log_text" label="日志">
            <el-input type="textarea" readonly :rows="25" v-model="form.log_text" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="type" label="类型">
              <dict-tag :options="dicts.funcallLogTypeOptions" :value="form.type"/>
<!--              <el-radio-group disabled v-model="form.type">-->
<!--                <el-radio-->
<!--                  v-for="dict in dicts.funcallLogTypeOptions"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.value"-->
<!--                >{{dict.label}}</el-radio>-->
<!--              </el-radio-group>-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="created_at" label="创建时间">
              <el-date-picker readonly v-model="form.created_at" type="datetime" placeholder="选择创建时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="updated_at" label="更新时间">
              <el-date-picker readonly v-model="form.updated_at" type="datetime" placeholder="选择更新时间" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { delFuncallLog, getFuncallLogDetail, listFuncallLog } from '@/api/openai-log'

export default {
  name: "FuncallLogIndex",
  components:{  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志列表数据
      mainList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chat_id: undefined,
        searchValue: undefined,
      },
      // 详情对话框可见性
      detailVisible: false,
      // 当前查看的日志详情数据
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询日志列表 */
    getList() {
      this.loading = true;
      listFuncallLog(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.mainList = [];
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 查看详情 */
    handleView(row) {
      let uniqueId=row.id
      getFuncallLogDetail({id:uniqueId}).then(res=>{
        this.form = res.data;
        this.detailVisible = true;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(() => {
        return delFuncallLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 复制代码成功 */
    clipboardSuccess() {
      this.$modal.msgSuccess("复制成功");
    },
  }
};
</script>

<style scoped>
/*
values are default one from jv-light template
*/
.my-awesome-json-theme {
  background: #fff;
  white-space: nowrap;
  color: #525252;
  font-size: 14px;
  font-family: Consolas, Menlo, Courier, monospace;

  .jv-ellipsis {
    color: #999;
    background-color: #eee;
    display: inline-block;
    line-height: 0.9;
    font-size: 0.9em;
    padding: 0px 4px 2px 4px;
    border-radius: 3px;
    vertical-align: 2px;
    cursor: pointer;
    user-select: none;
  }
  .jv-button { color: #49b3ff }
  .jv-key { color: #111111 }
  .jv-item {
    &.jv-array { color: #111111 }
    &.jv-boolean { color: #fc1e70 }
    &.jv-function { color: #067bca }
    &.jv-number { color: #fc1e70 }
    &.jv-number-float { color: #fc1e70 }
    &.jv-number-integer { color: #fc1e70 }
    &.jv-object { color: #111111 }
    &.jv-undefined { color: #e08331 }
    &.jv-string {
      color: #42b983;
      word-break: break-word;
      white-space: normal;
    }
  }
  .jv-code {
    .jv-toggle {
      &:before {
        padding: 0px 2px;
        border-radius: 2px;
      }
      &:hover {
        &:before {
          background: #eee;
        }
      }
    }
  }
}

</style>
